<template>

	<div class="pb50">
		<el-form size="small" ref="form" :model="form" label-width="150px">
			<!--基础-->
			<BasicIndex :form="form"></BasicIndex>
			<!--字段-->
			<Fields :form="form"></Fields>
		</el-form>

		<!--提交-->
		<div class="common-button-wrapper">
			<el-button size="small" @click="cancelFunc">取消</el-button>
			<el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
		</div>
	</div>
</template>
<script>
	import {
		add
	} from "@/api/plus/table.js";
	import BasicIndex from "./part/Basic.vue";
	import Fields from "./part/Fields.vue";
	export default {
		components: {
			/*基础设置*/
			BasicIndex,
			/*活动商品*/
			Fields,
		},
		data() {
			return {
				/*表单对象*/
				form: {
					name: "",
					/*默认排序*/
					sort: 100,
					/*商品源数据*/
					tableData: [],
				},
				/*是否正在加载*/
				loading: false,
			};
		},
		created() {},
		methods: {
			/*提交表单*/
			onSubmit() {
				let self = this;
				let flag = true;
				if (self.form.tableData == '') {
					ElMessage.error('字段不能为空');
					return
				}
				self.form.tableData.every((v) => {
					if (v.type != 'radio' && v.type != 'radio-group' && v.type != 'select') {
						if (!v.name) {
							flag = false
						}

					} else {
						if (!v.select_value) {
							flag = false;
						}
					}
				})
				if (!flag) {
					ElMessage.error('名字和备选值必填');
					return
				}
				self.$refs.form.validate((valid) => {
					if (valid) {
						let params = self.form;
						self.loading = true;
						add(params, true)
							.then((data) => {
								self.loading = false;
								ElMessage({
									message: "恭喜你，添加成功",
									type: "success",
								});
								self.cancelFunc();
							})
							.catch((error) => {
								self.loading = false;
							});
					}
				});
			},
			/*取消*/
			cancelFunc() {
				this.$router.back(-1);
			},
		},
	};
</script>